package com.cop4331.social;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * 
 * @author Matt Szoke
 * @LastUpdated: 3/5/2012 by Matt Szoke
 * 
 * Prompts user with a security question based on username
 * 
 */
public class SecurityActivity extends Activity {

	Button proceed;
	Button cancel;
	EditText un;
	TextView err;
	TextView question;

	// -----------------------------------------------
	// ONCREATE
	// -----------------------------------------------
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.security);

		// Buttons
		proceed = (Button) findViewById(R.id.b_proceed);
		cancel = (Button) findViewById(R.id.b_cancel);

		// User input text fields
		un = (EditText) findViewById(R.id.et_answer);

		// User prompt text fields
		err = (TextView) findViewById(R.id.err);
		question = (TextView) findViewById(R.id.tv_sec);

		// Retrieve information from previous activity (ForgotPassActivity)
		final Bundle b = getIntent().getExtras();
		final String password = b.getString("pass");
		final String ans = b.getString("ans");
		String sec = b.getString("sec");

		// Display Question to user
		question.setText(sec);

		// Gives functionality to "Proceed" button
		proceed.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				String answer = un.getText().toString();

				// If the answer is correct, display password
				if (answer.compareTo(ans) == 0) {
					UserFunctions uf = new UserFunctions();
					
					// Send email to list email, confirming password
					uf.sendEmail(b.getString("full"), b.getString("username"),
							password, b.getString("email"));
					err.setText("Email sent with login information!");
				}

				// Else display "WRONG ANSWER"
				else
					err.setText("Wrong Answer");

			}
		});

		// Cancel button, return to loginactivity
		cancel.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {

				Intent log = new Intent(getApplicationContext(),
						LoginActivity.class);
				startActivity(log);
				finish();

			}
		});
	}
}
